package com.gs.dao.jdbc.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.gs.bean.ImgNew;
import com.gs.dao.ImgNewDAO;
import com.gs.dao.jdbc.impl.rowmapper.ImgNewRowMapper;
import com.liang.log.Log;
import com.liang.util.Page;

public class ImgNewDAOImpl extends BaseDAOImpl implements ImgNewDAO {

	@Override
	public void addImgNew(ImgNew newImgNew) {
		String sql = "insert into imgnew_info(title,imgpath,describe) values(?,?,?)";
		List<Object> _agrs = new ArrayList<Object>();
		_agrs.add(newImgNew.getTitle());
		_agrs.add(newImgNew.getImgPath());
		_agrs.add(newImgNew.getDescribe());
		int type = Types.VARCHAR;
		jdbcTemplate.update(sql, _agrs.toArray(), new int[]{type,type,type});
	}

	@Override
	public void deleteImgNew(Integer id) {
		
		String sql = "delete imgnew_info where id = ?";
		jdbcTemplate.update(sql, new Object[]{id}, new int[]{Types.INTEGER});

	}

	@Override
	public ImgNew findImgNew(Integer id) {
		String sql = "select * from imgnew_info where id = ?";
		ImgNew obj = null;
		obj = (ImgNew)jdbcTemplate.queryForObject(sql,new Object[]{id}, new ImgNewRowMapper());
		return obj;
	}

	@Override
	public List<ImgNew> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<ImgNew> queryPage(ImgNew searchModel, int currentPage,
			int pageSize) {
		Page<ImgNew> page  = null;
		String _queryQuery = "select * from imgnew_info";
		StringBuffer _queryWhere = new StringBuffer();
		List<ImgNew> modules = null;
		if(null == searchModel){
			modules = jdbcTemplate.query(_queryQuery, new ImgNewRowMapper());
		}else{
			List<Object> _args = new ArrayList<Object>(0);
			List<Integer> _argTypeObjs = new ArrayList<Integer>(0);
			
			if(searchModel.getId() != null){
				_queryWhere.append(" id = ?");
				_args.add(searchModel.getId());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(searchModel.getTitle() != null){
				_queryWhere.append(" title like ?");
				_args.add('%'+searchModel.getTitle()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getImgPath() != null){
				_queryWhere.append(" imgPath like ?");
				_args.add('%'+searchModel.getImgPath()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getDescribe() != null){
				_queryWhere.append(" describe like ?");
				_args.add('%'+searchModel.getDescribe()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			
			
			if(!_queryWhere.toString().equals("")){
				_queryWhere.insert(0, " where");
			}
			_queryQuery += _queryWhere.toString();
			
			Log.info("sql:"+_queryQuery);
			int[] _argTypes = new int[_argTypeObjs.size()];
			for(int i = 0 ;  i < _argTypeObjs.size() ; i++){
				_argTypes[i] = _argTypeObjs.get(i);
			}
			modules = jdbcTemplate.query(_queryQuery, _args.toArray(), _argTypes, new ImgNewRowMapper());
		}
		
		if(null == modules){
			modules = new ArrayList<ImgNew>(0);
		}
		
		page = new Page<ImgNew>(modules , currentPage , pageSize , modules.size());
		return page;
	}

	@Override
	public void updateImgNew(ImgNew updateImgNew) {
		// TODO Auto-generated method stub

	}

}
